#pragma once
#include <atomic>

class CSpinLock
{
public:
    CSpinLock() : flag(false) {}
    ~CSpinLock() {}

    void lock()
    {
        bool expect = false;

        while (!flag.compare_exchange_weak(expect, true))
        {
            expect = false;
            asm volatile("pause\n" ::: "memory"); // _mm_pause();
        }
    }

    void unlock()
    {
        flag.store(false);
    }

private:
    std::atomic<bool> flag;
};

class CFakeLock
{
public:
    CFakeLock() {}
    ~CFakeLock() {}
    void lock() {}
    void unlock() {}
};

// using namespace std;

// class CSpinLock : public atomic_flag
// {
// public:
//     CSpinLock(){};
//     ~CSpinLock(){};

//     void lock()
//     {
//         while (test_and_set(std::memory_order_acquire))
//         {
//             asm volatile("pause\n" ::: "memory");
//         }
//     }

//     void unlock()
//     {
//         clear(std::memory_order_release);
//     }
// };